home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_1 / cpdist / main.c < prev    next >
C/C++ Source or Header  |  1994-01-12  |  6KB  |  255 lines

  1. /*
  2.  *  MAIN.C
  3.  */
  4.  
  5. /*
  6.  * (c)Copyright 1992-93 by Tobias Ferber.
  7.  *
  8.  * This file is part of CPDIST.
  9.  *
  10.  * CPDIST is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published
  12.  * by the Free Software Foundation; either version 1 of the License,
  13.  * or (at your option) any later version.
  14.  *
  15.  * CPDIST is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with CPDIST; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "cpdist.h"
  30. #include "filecopy.h"
  31.  
  32. /* globals */
  33. unsigned long global_opts; /* command-line options */
  34. long global_buffersize;    /* size of our copy buffer */
  35.  
  36. void display_version_information(void)
  37. {
  38.   static char license[]=
  39.     "CPDIST is free software; you can redistribute it and/or modify\n"
  40.     "it under the terms of the GNU General Public License as published\n"
  41.     "by the Free Software Foundation; either version 1 of the License,\n"
  42.     "or (at your option) any later version.\n"
  43.     "\n"
  44.     "CPDIST is distributed in the hope that it will be useful,\n"
  45.     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  46.     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
  47.     "GNU General Public License for more details.\n"
  48.     "\n"
  49.     "You should have received a copy of the GNU General Public License\n"
  50.     "along with CPDIST; see the file COPYING.  If not, write to the\n"
  51.     "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  52.     ;
  53.  
  54.   puts("CPDIST Version " VERSION " (compiled " __DATE__ ", " __TIME__ ")\n"
  55.        "(c)Copyright 1992-93 by Tobias Ferber, ukjg@dkauni2.bitnet\n");
  56.  
  57.   puts(license);
  58. }
  59.  
  60. main(int argc, char *argv[])
  61. {
  62.   int badopt= 0;
  63.  
  64.   /* these are safe in here */
  65.   char distfile[MAXIMUM_PATHNAME_LENGTH];
  66.   char destination[MAXIMUM_PATHNAME_LENGTH];
  67.  
  68.   int haveapath= 0;
  69.  
  70. #ifdef __MSDOS__
  71.  
  72.   /* filenames on MS-DOS systems look very ugly: all uppercase and
  73.    * backslashes.  Perform some cosmetics. */
  74.  
  75.   whoami= "cpdist";
  76.  
  77. #else /* !__MSDOS__ */
  78.   whoami= argv[0];
  79.  
  80. #endif /* __MSDOS__ */
  81.  
  82.   /* this is where the echo()d messages go to */
  83.   ferr= stderr;
  84.  
  85.   strcpy(distfile, DEFAULT_DISTFILE);
  86.  
  87.   /* init the globals */
  88.   global_opts= 0L;
  89.   global_buffersize= MAXIMUM_BUFFERSIZE;
  90.  
  91.   while(--argc>0 && !badopt)
  92.   {
  93.     char *arg= *(++argv);
  94.  
  95.     if(*arg==OPTION_PREFIX)
  96.     {
  97.       if(arg[1]==OPTION_PREFIX)
  98.     arg= convert_args(*argv);
  99.  
  100.       switch(*++arg)
  101.       {
  102.  
  103. /*-a*/  case 'a':
  104.       global_opts |= OPT_REPLACEALL;
  105.       break;
  106.  
  107. /*-b*/  case 'b':
  108.       if(arg[1]) ++arg;
  109.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  110.  
  111.       if(arg && *arg)
  112.       { global_buffersize= atol(arg);
  113.         if(global_buffersize < 0L)
  114.         { echo("Illegal buffer size %ld (must be non-negative)",
  115.         global_buffersize);
  116.           badopt= 10;
  117.         }
  118. #if defined(__TURBOC__)
  119.         if(global_buffersize > MAXIMUM_BUFFERSIZE)
  120.         { echo("Buffer size is limited to %dK.  Will use this upper limit.",
  121.         MAXIMUM_BUFFERSIZE/1024);
  122.         }
  123. #endif /* __TURBOC__ */
  124.       }
  125.       else
  126.       { echo("Missing buffer size after '%s' option",*argv);
  127.         badopt= 10;
  128.       }
  129.       break;
  130.  
  131. /*-c*/  case 'c':
  132.       global_opts |= OPT_CHECKEXISTS;
  133.       break;
  134.  
  135. /*-D*/  case 'D':
  136.       if(arg[1]) ++arg;
  137.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  138.  
  139.       if(arg && *arg)
  140.       { if(!ln_addnode(arg))
  141.         { echo("Not enough bits in the system free pool!");
  142.           badopt= 10;
  143.         }
  144.       }
  145.       else
  146.       { echo("Missing distribution key after '%s' option.",*argv);
  147.         badopt= 10;
  148.       }
  149.       break;
  150.  
  151. /*-E*/  case 'E':
  152.       if(arg[1]) ++arg;
  153.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  154.  
  155.       if(arg && *arg)
  156.       { FILE *fp;
  157.         if( fp= fopen(arg,"w") )
  158.         { if(ferr && ferr!=stderr)
  159.         fclose(ferr);
  160.           ferr= fp;
  161.         }
  162.         else
  163.         { perror(whoami);
  164.           echo("Can't redirect output to `%s'.",arg);
  165.           badopt= 10;
  166.         }
  167.       }
  168.       else
  169.       { echo("Missing filename or device after '%s' option.",*argv);
  170.         badopt= 10;
  171.       }
  172.       break;
  173.  
  174. /*-f*/  case 'f':
  175.       if(arg[1]) ++arg;
  176.       else arg= (--argc > 0) ? *(++argv) : (char *)0L;
  177.        if(arg && *arg)
  178.         strcpy(distfile,arg);
  179.       else
  180.       { echo("Missing filename after '%s' option", *argv);
  181.         badopt= 10;
  182.       }
  183.       break;
  184.  
  185. /*-?*/  case '?':
  186. /*-h*/  case 'h': case 'H':
  187.       display_args();
  188.       puts("\nRefer to the docs for further information!");
  189.       badopt= 1; /* hack: means exit. */
  190.       break;
  191.  
  192. /*-i*/  case 'i':
  193.       global_opts |= OPT_IGNOREERRORS;
  194.       break;
  195.  
  196. /*-k*/  case 'k':
  197.       global_opts |= OPT_KEEPGOING;
  198.       break;
  199.  
  200. /*-n*/  case 'n':
  201.       global_opts |= OPT_DRYRUN;
  202.       break;
  203.  
  204. /*-q*/  case 'q':
  205. /*-s*/  case 's':
  206.       global_opts |= OPT_SILENT;
  207.       break;
  208.  
  209. /*-v*/  case 'v':
  210.       display_version_information();
  211.       break;
  212.  
  213. /*..*/  default:
  214.       echo("bad option '-%s'.",arg);
  215.       badopt= 10;
  216.       break;
  217.       }
  218.     }
  219.     else /* pathname */
  220.     {
  221.       if(*arg)
  222.       {
  223.         int l= strlen(arg)-1;
  224.  
  225.         if(arg[l]==':' || arg[l]==PSLASH)
  226.           strcpy(destination, arg);
  227.         else
  228.           sprintf(destination,"%s%c",arg,PSLASH);
  229.       }
  230.       else destination[0]= '\0';
  231.  
  232.       if(haveapath)
  233.       { echo("More than one destination path in your command line.");
  234.         echo("Will use %s now.", *destination ? destination : "current directory");
  235.       }
  236.       else haveapath++;
  237.     }
  238.   } /* wend */
  239.  
  240.   if(!badopt)
  241.   {
  242.     if(!haveapath && CP_DRYRUN)
  243.       destination[haveapath++]= '\0';
  244.  
  245.     if(haveapath) /* let's roll! */
  246.       badopt= prepare_distribution(distfile, destination);
  247.     else
  248.     { echo("Missing destination path.");
  249.       badopt= 10;
  250.     }
  251.   }
  252.   ln_purge();
  253.   exit(badopt);
  254. }
  255.